package util.hashing;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.LinkedList;

public class GeneralHash {

	/**
	 * @param args
	 */
	
	
	
	private Hashtable<String,Object> hash = new Hashtable<String,Object>();
	
	
	
	
	public String getElement(String key){
		
		String l ="";
		
		
		if(key==null) return null;
		String temp[] = key.split("\t");
		Hashtable<String,Object> temporal= hash;
		
		for(int i =0 ; i < temp.length; i++){
			
			temporal = (Hashtable<String, Object>) temporal.get(temp[i]);
			
			
		}
		
		Enumeration<String> keys = temporal.keys();
		
		
		
		while(keys.hasMoreElements()){
			
			l = l + keys.nextElement() + "\t";
			
		}
		
		return l;
	}
	public void insert(String key, String value) {

		if (hash.containsKey(key)) {

			Object o = hash.get(key);

			Hashtable<String, Object> temp = (Hashtable<String, Object>) o;
			Hashtable<String, Object> temp2 = new Hashtable<String, Object>();
			
			if (value != null && !temp.containsKey(value)) {

				temp.put(value, temp2 );

			} else {

			}

			hash.put(key, temp);

		} else {

			Hashtable<String, Object> temp2 = new Hashtable<String, Object>();

			if (value != null) {
				temp2.put(value, temp2);
			}

			hash.put(key, temp2);

		}

	}
	
	
	/**Add items in the String line
	 * 
	 * Items are separated using tabs and each indent level implies subsumption in the hierarchy
	 * 
	 * 
	 * @param line
	 */
	public void addItems(String line) {

		if (line == null)
			return;

		String temp[] = line.split("\t");

		for (int i = 0; i < temp.length; i++) {

			Hashtable<String, Object> temporal = hash;

			for (int j = 0; j <= i; j++) {
				Hashtable<String, Object> temporal2;
				if (temporal.containsKey(temp[j])) {

					temporal = (Hashtable<String, Object>) temporal
							.get(temp[j]);
					
									
				} else {
					temporal2 = new Hashtable<String, Object>();
					temporal.put(temp[j], temporal2);
					temporal = temporal2;
				}

			}

		}

	}
	
	public String toString1() {
		String a = "";

		Enumeration<String> keys = hash.keys();

		while (keys.hasMoreElements()) {
			String key = keys.nextElement();

			a = a + "[\"" + key + "\"";
			a = printHelper(hash.get(key), a);

			if (keys.hasMoreElements()) {
				a = a + "],\n";
			} else {
				a = a + "]\n";

			}

		}

		return a;

	}

	private String printHelper(Object object, String a) {
		// TODO Auto-generated method stub

		if (object != null) {

			Hashtable<String, Object> temp = (Hashtable<String, Object>) object;

			if (temp.size() > 0) {

				a = a + ",[";

				Enumeration<String> keys = temp.keys();

				boolean first = true;
				while (keys.hasMoreElements()) {

					String key = keys.nextElement();
				//	System.out.println(key + "\t" + a);

					if (!first) {
						a = a + ",\"" + key + "\"";

					} else {
						a = a + "\"" + key + "\"";
						first = false;
					}

					Hashtable<String, Object> temp2 = (Hashtable<String, Object>) temp
							.get(key);
					//System.out.println(key + "\t" + temp2.size() + a);
					if (temp2.size() > 0) {
					 a= 	printHelper(temp2, a);

					} else {

					}

				}

				a = a + "]";
			
			}

		}
		
		return a;

	}

	
	public void iniGeneralHash(LinkedList<String> list){
		
		
		for(int i =0 ; i < list.size(); i++){
			
		
			addItems(list.get(i));
			
		}
		
		
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String a = "a	b	c";
		String b= "a	b	d";
		String d= "a	b";
		
		String c= "c";
		String e= "c	e";
		
		GeneralHash hash= new GeneralHash();
		
		hash.addItems(a);
		hash.addItems(b);
		hash.addItems(c);
		hash.addItems(e);
		hash.addItems(d);
		
		
		
		//System.out.println(hash.getElement("a"));
		
		
		
			System.out.println(hash.toString1());
		
	}

}
